<?php

namespace app\index\controller;

use app\common\controller\Log;
use app\common\model\Member;
use app\common\model\MemberFinance;
use app\common\model\MemberPackages;
use app\common\model\Packages;
use app\common\model\UcenterMember;
use think\Db;
use app\common\model\Traders as tradersModel;
use app\common\api\controller\Bonus;
use app\common\api\controller\User as UserApi;
use app\common\api\controller\Error;
use think\Request;
use think\Validate;


class MemberGroups extends Home
{
    private static $model;

    protected $beforeActionList = [
        'check_load'
    ];

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        self::$model = new \app\common\model\MemberGroups();
    }

    /**
     * 群组列表
     */
    public function lists()
    {
        $uid = $this->uid;
        $page = input('page', 1);
        $start = ($page - 1) * 10;

        $lists = self::$model->where('status', 1)
            ->where('uid', $uid)
            ->order('update_time desc')
            ->limit($start, 10)
            ->select();

        if (count($lists) > 0) {
            foreach ($lists as $k => $v) {
                $v->member;
            }
        }

        $total = self::$model->where('status', 1)
            ->where('uid', $uid)
            ->count();

        $this->apiSuccess('群组列表', ['lists' => $lists, 'total' => $total]);
    }

    /**
     * 待添加好友列表
     */
    public function newLists()
    {
        $uid = $this->uid;
        $page = input('page', 1);
        $start = ($page - 1) * 10;
        $tuidsArr = self::$model->where('status', 1)
            ->where('uid', $uid)
            ->column('tuid');
        array_push($tuidsArr, $uid);
        $lists = Member::field('uid,usernumber,realname,icon')->where('status', 1)
            ->where('uid', 'not in', $tuidsArr)
            ->order('uid asc')
            ->limit($start, 10)
            ->select();

        $total = Member::where('status', 1)
            ->where('uid', 'not in', $tuidsArr)
            ->count();

        $this->apiSuccess('未加好友列表', ['lists' => $lists, 'total' => $total]);
    }


    /**
     * Notes:群组详情
     * User: Administrator
     * Date: 2023/9/13
     * Time: 11:38
     */
    public function one()
    {
        $id = input('id');
        $rowInfo = self::$model->find($id);
        if (!$rowInfo) {
            $this->apiError('该群组不存在');
        }

        $number = \app\common\model\MemberGroupsChat::where('group_id', $id)->count();
        $rowInfo->memberCounts = (string)$number;

        $this->apiSuccess('群组信息', $rowInfo);
    }

    /**
     * 添加好友
     */
    public function create()
    {
        $tuid = input('tuid');
        if (!$tuid) {
            $this->apiError('参数缺失！');
        }
        $uid = $this->uid;

        $finfo = self::$model->where('uid', $uid)->where('tuid', $tuid)->find();
        if ($finfo) {
            $this->apiSuccess('该会员已添加！');
        }

        $data = [
            'uid' => $uid,
            'tuid' => $tuid
        ];

        $ret = self::$model->create($data);
        if (!$ret->id) {
            $this->apiError('添加失败！');
        }

        $this->apiSuccess('添加成功！');
    }
}